/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var formData = {
        pageStart: pages.start,
        pageLimit: pages.limit,
    };
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'yqh/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formList = res.data;
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        if (d[i].bg == '') {
            h += '<td>-</td>';
        } else {
            h += '<td><img src="' + d[i].bg + '" height="100"/></td>';
        }
        h += '<td>' + d[i].invited + '</td>';
        h += '<td>' + d[i].canjia_time + '</td>';
        h += '<td>' + d[i].canjia_dizhi + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'yqh-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '<i class="Hui-iconfont"></i>';
        h += '</a>';
        h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="invited' + d[i].id + '" value="' + d[i].invited + '"/>';
        h += '<input type="hidden" id="canjia_time' + d[i].id + '" value="' + d[i].canjia_time + '"/>';
        h += '<input type="hidden" id="canjia_dizhi' + d[i].id + '" value="' + d[i].canjia_dizhi + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var invited = $('#invited' + id).val();
    var canjia_time = $('#canjia_time' + id).val();
    var canjia_dizhi = $('#canjia_dizhi' + id).val();
    tt = tt ? escape(tt) : '';
    invited = invited ? escape(invited) : '';
    canjia_time = canjia_time ? escape(canjia_time) : '';
    canjia_dizhi = canjia_dizhi ? escape(canjia_dizhi) : '';
    url += '&title=' + tt;
    url += '&invited=' + invited;
    url += '&canjia_time=' + canjia_time;
    url += '&canjia_dizhi=' + canjia_dizhi;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $('#title')[0].value = title;
    $('#invited')[0].value = invited;
    $('#canjia_time')[0].value = canjia_time;
    $('#canjia_dizhi')[0].value = canjia_dizhi;
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '" style="width: 150px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//修改
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
        invited: $('#invited').val(),
        canjia_time: $('#canjia_time').val(),
        canjia_dizhi: $('#canjia_dizhi').val(),
        bg: $('#img').val(),
    };
    console.log(formData);
    if (!formData.title) {
        msgPop({msg: '名称必须的'}); return
    }
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'yqh/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            //window.location.href = '';
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'yqh/del',
            data: {aid: getAid(), id: id},
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}